package com.atao.baokede.mapper;

import com.atao.baokede.pojo.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.List;

@Mapper
public interface UserMapper {
    //根据用户名查询用户
    @Select("select * from user where username=#{username}")
     User findByUserName(String username);

    //注册用户
    @Insert("insert into user(username,password,email,create_time,update_time) values(#{username},#{password},#{email},now(),now())")
    void register(String username, String password,String email);

    //根据用户id查询用户
    @Select("select * from user where id=#{userId}")
    User findByUserId(Long userId);

    //更新用户信息
    @Update("update user set username=#{username},sex=#{sex},update_time=now() where id=#{id}")
    void update(User user);

    //更新图片
    @Update("update user set user_pic=#{avatarUrl},update_time=now() where id =#{userId}")
    void updateAvatar(String avatarUrl, Long userId);

    //根据邮箱查询用户
    @Select("select * from user where email= #{email}")
    User findByUserEmail(String email);

    //更新密码
    @Update("update user set password= #{newPwd},update_time=now() where email = #{email}")
    void updatePwd(String email,String newPwd);

    //更新余额
    @Update("update user set balance= balance + #{balance},update_time=now() where id = #{userId}")
    void updateBalance(BigDecimal balance, Long userId);

    //减去余额
    @Update("update user set balance= balance - #{requestAmount},update_time=now() where id = #{userId}")
    void reduceBalance(BigDecimal requestAmount, Long userId);

    //根据用户名查询用户id
    @Select("select id from user where username like concat('%',#{userKeyword},'%')")
    List<Long> findUsersByUserKeyword(String userKeyword);
}